package com.example.zbh.dsliao.model;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.db.DatabaseHelper;
import com.example.zbh.dsliao.db.entity.CarrouselPic;
import com.example.zbh.dsliao.db.entity.ForYouRecommend;
import com.example.zbh.dsliao.db.entity.HotSongMenu;
import com.example.zbh.dsliao.db.entity.NewSong;
import com.example.zbh.dsliao.db.entity.NewSongImg;
import com.example.zbh.dsliao.db.entity.RadioSet;
import com.example.zbh.dsliao.db.entity.SongRecommend;
import com.example.zbh.dsliao.model.entity.SongRecommedResult;
import com.example.zbh.dsliao.model.entity.SongRecommendContent;
import com.example.zbh.dsliao.model.modelinterface.RecommendModelListener;
import com.example.zbh.dsliao.presenter.presenterinterface.RecommentPresenterListener;
import com.example.zbh.dsliao.utils.net.VolleySingleton;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zbh on 15/12/7.
 */
public class RecommendModel implements RecommendModelListener {
    private List<CarrouselPic> lists;
    @Override
    public void executetCarousel(String url, final RecommentPresenterListener presenterListener) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        if (response != null && response.has("pic")) {
                            try {
                                JSONArray jsonArray = response.getJSONArray("pic");
                                lists = new ArrayList<CarrouselPic>();
                                Gson gson = new Gson();
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    String str = jsonArray.getString(i);
                                    CarrouselPic c = gson.fromJson(str, CarrouselPic.class);
                                    lists.add(c);

                                }
                                presenterListener.sendCarouselResult(lists);

//                                adapter.addData(lists);
////                                //删除所有
//                                DatabaseHelper.getInstance().deleteAllCarousel();
////                                //添加数据
//                                DatabaseHelper.getInstance().saveCarousel(lists);
//                                handler.postDelayed(r, 2000);
//                                //创建小圆点
//                                initCircle();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        VolleySingleton.getInstance().addRequest(request);

    }

    @Override
    public void executetForYouRecommend(String url, final RecommentPresenterListener presenterListener) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        if (response != null && response.has("result")) ;
                        try {
                            JSONObject object = response.getJSONObject("result");
                            if (object != null) {
                                JSONArray array = object.getJSONArray("list");
                                List<ForYouRecommend> list = new ArrayList<ForYouRecommend>();
                                Gson gson = new Gson();
                                for (int i = 0; i < array.length(); i++) {
                                    String str = array.getString(i);
                                    ForYouRecommend r = gson.fromJson(str, ForYouRecommend.class);
                                    list.add(r);
                                }
                                presenterListener.sendForYouRecommendResult(list);
//                                forYouAdapter.addData(list);
//                                //删除所有
//                                DatabaseHelper.getInstance().deleteAllForYouRrcommend();
//                                //添加数据
//                                DatabaseHelper.getInstance().saveForYouRecommend(list);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        VolleySingleton.getInstance().addRequest(request);


    }

    @Override
    public void executetHotSongMenu(String url, final RecommentPresenterListener presenterListener) {

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        if (response != null && response.has("data")) {
                            try {
                                JSONArray array = response.getJSONArray("data");
                                Gson gson = new Gson();
                                List<HotSongMenu> list = new ArrayList<HotSongMenu>();
                                for (int i = 0; i < array.length(); i++) {
                                    String str = array.getString(i);
                                    HotSongMenu h = gson.fromJson(str, HotSongMenu.class);
                                    list.add(h);
                                }
                                presenterListener.sendHotSongMenuResult(list);
//                                hotAdapter.addData(list);
//                                //删除所有
//                                helper.deleteAllHotSongMenu();
//                                //添加数据
//                                helper.saveHotSongMenu(list);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        VolleySingleton.getInstance().addRequest(request);
    }

    @Override
    public void executetSongRecommend(String url, final RecommentPresenterListener presenterListener) {
        VolleySingleton.getInstance().addStringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SongRecommedResult srr = gson.fromJson(response, SongRecommedResult.class);
                if (srr != null) {
                    List<SongRecommendContent> sr = srr.getContent();
                    if (sr != null && sr.size() > 0) {
                        for (SongRecommendContent s : sr) {
                            List<SongRecommend> list = s.getSong_list();
                            if (list != null && list.size() > 0) {
                                presenterListener.sendSongRecommendResult(list);
//                                songAdapter.addData(list);
//                                //删除所有
//                                helper.deleteAllSongRecommend();
//                                //添加数据
//                                helper.saveSongRecommend(list);
                            }
                        }
                    }
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });


    }

    @Override
    public void executetNewSongImg(String url, final RecommentPresenterListener presenterListener) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        if (response != null && response.has("collection")) {
                            try {
                                JSONObject object = response.getJSONObject("collection");
                                presenterListener.sendNewSongImgResult(object);
//                                String title = object.getString("title");
//                                String pic = object.getString("pic");
//                                String message = object.getString("message");
//                                titleTv.setText(title);
//                                messageTv.setText(message);
//                                getBitmap(pic, newSongImg);
//                                //删除所有
//                                //helper.deleteAllNewSongImg();
//                                //添加数据
//                                newSongImage = new NewSongImg(null, title, pic, message);
//                                helper.saveNewSongImg(newSongImage);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }

//                    private void getBitmap(String url, NetworkImageView imageView) {
//                        imageView.setDefaultImageResId(R.mipmap.music_cache_icon);
//                        imageView.setErrorImageResId(R.mipmap.music_cache_icon);
//                        imageView.setImageUrl(url, imageLoader);
//                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        VolleySingleton.getInstance().addRequest(request);


    }

    @Override
    public void executetNewSong(String url, final RecommentPresenterListener presenterListener) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        if (response != null && response.has("plaze_album_list")) ;
                        try {
                            JSONObject jsonObject = response.getJSONObject("plaze_album_list");
                            if (jsonObject != null && jsonObject.has("RM")) {
                                JSONObject object = jsonObject.getJSONObject("RM");
                                if (object != null && object.has("album_list")) {
                                    JSONObject o = object.getJSONObject("album_list");
                                    if (o != null && o.has("list")) {
                                        JSONArray array = o.getJSONArray("list");
                                        List<NewSong> list = new ArrayList<NewSong>();
                                        Gson gson = new Gson();
                                        for (int i = 0; i < array.length(); i++) {
                                            String str = array.getString(i);
                                            NewSong n = gson.fromJson(str, NewSong.class);
                                            list.add(n);
                                        }
                                        presenterListener.sendNewSongResult(list);
//                                        newAdapter.addData(list);
//                                        //删除所有
//                                        helper.deleteAllNewSong();
//                                        //添加数据
//                                        helper.saveNewSong(list);
                                    }
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        VolleySingleton.getInstance().addRequest(request);
    }

    @Override
    public void executetRadioSet(String url, final RecommentPresenterListener presenterListener) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        if (response != null && response.has("list")) {
                            try {
                                JSONArray array = response.getJSONArray("list");
                                Gson gson = new Gson();
                                List<RadioSet> list = new ArrayList<RadioSet>();
                                for (int i = 0; i < array.length(); i++) {
                                    String str = array.getString(i);
                                    RadioSet r = gson.fromJson(str, RadioSet.class);
                                    list.add(r);
                                }
                                presenterListener.sendRadioSetResult(list);
//                                radioAdapter.addData(list);
//                                //删除所有
//                                helper.deleteAllRadioSet();
//                                //添加数据
//                                helper.saveRadioSet(list);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        VolleySingleton.getInstance().addRequest(request);
    }
}
